package org.yoki.edu.bg.radius.web.api.impl.rest.server;

import org.yoki.edu.bg.radius.application.server.ServerApplication;
import org.yoki.edu.bg.radius.core.server.domain.Server;
import org.yoki.edu.bg.radius.vo.server.ServerVO;
import org.yoki.edu.bg.radius.web.api.command.IdsCommand;
import org.yoki.edu.bg.radius.web.api.impl.rest.BaseController;
import org.yoki.edu.bg.radius.web.api.impl.rest.server.assembler.ServerAssembler;
import org.yoki.edu.bg.radius.web.api.server.ServerService;
import org.yoki.edu.bg.radius.web.api.server.command.ServerCreateCommand;
import org.yoki.edu.bg.radius.web.api.server.command.ServerPageCommand;
import org.yoki.edu.bg.radius.web.api.server.command.ServerUpdateCommand;
import com.yunzhitx.cloud.common.model.InvokeResult;
import com.yunzhitx.cloud.common.model.PageResult;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;


/**
 * 代码生成器自动生成
 * Date:2018-1-31 15:03:16
 *
 * @author
 */
@RestController
public class ServerController extends BaseController implements ServerService {

    @Inject
    private ServerApplication application;

    /* ************************************************************************************ *
       ***************************************  增  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult<ServerVO> add(@RequestBody @Valid ServerCreateCommand command) {
        Server entity = ServerAssembler.toServer(command);
        entity.setOperatorId(getLoginId());
        entity.setOperatorName("测试管理员");
        ServerVO vo = application.create(entity);
        return InvokeResult.ok(vo);
    }

    /* ************************************************************************************ *
       ***************************************  删  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult deleteById(@RequestParam("id") @NotNull Integer id) {
        application.deleteById(id);
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult deleteByIds(@RequestBody @Valid IdsCommand command) {
        application.deleteByIds(command.getIdList());
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult deleteByIpAddr(@RequestParam("ipAddr") @NotBlank String ipAddr) {
        application.deleteByIpAddr(ipAddr);
        return InvokeResult.ok();
    }

    /* ************************************************************************************ *
       ***************************************  改  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult<ServerVO> update(@RequestBody @Valid ServerUpdateCommand command) {
        Server server = ServerAssembler.toServer(command);
        server.setOperatorId(getLoginId());
        server.setOperatorName("测试管理员");
        application.update(server);
        return InvokeResult.ok();
    }

    @Override
    public InvokeResult updateWorkState(@RequestParam("id") @NotNull Integer id,
                                        @RequestParam("isWork") @NotNull Boolean isWork) {
        Server server = new Server(id);
        server.setIsWork(isWork);
        server.setOperatorId(getLoginId());
        server.setOperatorName("测试管理员");
        Date currentDate = new Date();
        server.setUpdateDate(currentDate);
        application.update(server);
        return InvokeResult.ok();
    }

    /* ************************************************************************************ *
       ***************************************  查  ***************************************
    * ************************************************************************************* */

    @Override
    public InvokeResult<ServerVO> getDetailById(@RequestParam("id") @NotNull int id) {
        ServerVO vo = application.retriveDetailById(id);
        return buildInvokeResult(vo);
    }

    @Override
    public InvokeResult<PageResult<ServerVO>> getPage(PageResult page, @Valid ServerPageCommand command) {
        Server s = ServerAssembler.toServer(command);
        page = application.retrivePageByQuery(page, s);
        return buildPageInvokeResult(page);
    }

    @Override
    public InvokeResult<PageResult<ServerVO>> getList() {
        return buildListInvokeResult(application.retriveList());
    }

}